package com.dh.product.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.transaction.Transactional;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dh.product.common.DecreaseStockInput;
import com.dh.product.common.ProductInfoOutput;
import com.dh.product.dao.ProductDao;
import com.dh.product.entity.ProductInfo;
import com.dh.product.enums.ProductStatesEnum;
import com.dh.product.enums.ResultEnum;
import com.dh.product.exception.ProductException;
import com.dh.product.service.ProductService;
import com.dh.product.utils.JsonUtil;

@Service
public class ProductServiceImpl implements ProductService {

	@Autowired
	private ProductDao productDao;

	@Autowired
	private AmqpTemplate amqpTemplate;

	@Override
	public List<ProductInfo> findUpAll() {
		return productDao.findByProductStatus(ProductStatesEnum.UP.getCode());
	}

	@Override
	public List<ProductInfoOutput> findByProductIdIn(List<String> productIdList) {
		return productDao.findByProductIdIn(productIdList).stream().map(e -> {
			ProductInfoOutput output = new ProductInfoOutput();
			BeanUtils.copyProperties(e, output);
			return output;
		}).collect(Collectors.toList());
	}

	@Override
	public void decreaseStock(List<DecreaseStockInput> decreaseStockInputs) {
		List<ProductInfo> productInfoList = decreaseStockProcess(decreaseStockInputs);
		//发送mq消息
		List<ProductInfoOutput> productInfoOutputList = productInfoList.stream().map(e->{
			ProductInfoOutput output = new ProductInfoOutput();
			BeanUtils.copyProperties(e, output);
			return output;
		}).collect(Collectors.toList());
		try {
//			amqpTemplate.convertAndSend("productInfo",JacksonUtil.obj2json(productInfoOutputList));
			amqpTemplate.convertAndSend("productInfo",JsonUtil.toJson(productInfoOutputList));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Transactional
	public List<ProductInfo> decreaseStockProcess(List<DecreaseStockInput> decreaseStockInputs) {
		List<ProductInfo> productInfos = new ArrayList<>();
		for (DecreaseStockInput decreaseStockInput : decreaseStockInputs) {
			Optional<ProductInfo> productInfoOptional = productDao.findById(decreaseStockInput.getProductId());
			//判断商品是否存在
			if (!productInfoOptional.isPresent()) {
				throw new ProductException(ResultEnum.PRODUCT_NOT_EXIST);
			}
			ProductInfo productInfo = productInfoOptional.get();
			//库存是否足够
			Integer result = productInfo.getProductStock() - decreaseStockInput.getProductQuantity();
			if (result < 0) {
				throw new ProductException(ResultEnum.PRODUCT_STOCK_ERROR);
			}
			productInfo.setProductStock(result);
			productDao.save(productInfo);
			productInfos.add(productInfo);
		}
		return productInfos;
	}

}
